<?php
namespace Factory\Controller;
use Think\Controller;
use Mcit\Rbacs;

class LoginController extends Controller {

	public function index(){

		//获取浏览器版本
        $explore = $_SERVER["HTTP_USER_AGENT"];
//        var_dump($explore);
        if(strpos($explore,"MSIE 7.0") || strpos($explore,"MSIE 6.0") ){
            layout(false);
            $this->display("Public:error");
            exit;
        }
		layout(false);
		$token = rand(1000,9999);
        session("LoginToken",$token);
        session("LoginError",0);
        $this->token  = $token;
        $this->action = U("Login/check");
        $this->index  = U("Index/index");
		$this->display();
        //exit;
        //echo sha1('12qwaszx');
	}

	public function check(){

		$errorNum = 2;
		if(session("LoginError")>$errorNum){
            $verify = I("verify");
            $checkVerify = new \Think\Verify();  
            if(!$checkVerify->check($verify)){
                $result['verify']   = 1;
                $result['state']    = 0;
                $result['msg']      = "验证码不对";
                echo json_encode($result);
                exit;
            }
        }
        $fac_password = base64_decode(I('fac_password'));
        $token    = substr($fac_password,0,4);
        $fac_password = substr($fac_password,4);
        $logintype = I('logintype');
        if(session("LoginToken") == $token){
            
            $fac_login_name = I('fac_login_name');
            
            $result = $this->_checkUser($fac_login_name,$fac_password,$logintype);
            
            if($result['state']==0){
                session("LoginError",session("LoginError")+1);
                if(session("LoginError")>$errorNum){
	                $result['verify'] = 1;   
	            }else{
	                $result['verify'] = 0;
	            }
            }
            
            echo json_encode($result);
            exit;
        
        }else{
            $result['state']    = 0;
            $result['msg']      = "不对哦亲,您无法验证登录";
            echo json_encode($result);
            exit;
        }
	}

	public function test(){
		exit;
	}

	public function verify(){
		$Verify = new \Think\Verify();
		$Verify->length   = 4;
		$Verify->imageW   = 98;
		$Verify->imageH   = 30;
		$Verify->codeSet  = '0123456789';
		$Verify->useNoise = false;
		$Verify->useCurve = true;
		$Verify->fontSize = 13;
		$Verify->entry();
	}

	protected function _checkUser($fac_login_name,$fac_password){

        $map['fac_login_name'] = $fac_login_name;
        $userInfo = M('fac')->where($map)->find();

		if( false == $userInfo ){
            $return['state'] = 0;
            $return['msg']   = "用户不存在！";
            return $return;
        }

        if( $userInfo['fac_password'] != $fac_password && $userInfo['type'] == 2 || $userInfo['fac_password'] != sha1($fac_password) && $userInfo['type'] == 3){
    		$return['state'] = 0;
			$return['msg']   = '密码错误！';
        }else{
        	$return['state'] = 1;
            $return['msg']   = "欢迎进入系统！";
            $role = $userInfo['role'];
            session('role',$role);
            $auth = Rbacs::getAuth($role);
            session('auth',$auth);
            session('uid',$userInfo['id']);
            session('pid',$userInfo['pid']);
            $fac_key = M("fac")->where("id='".$userInfo['pid']."'")->getField("fac_login_name");
            session('fac_key',$fac_key);
            session('fac_login_name',$fac_login_name);
            session('fac_name',$userInfo['fac_name']);
            session('department',M("yy_role")->where("id='".$role."'")->getField("name"));
            //fac_type字段用于区分厂商与运营商：2--厂商  3--运营商
            session("fac_type",$userInfo['type']);
            session('logo',$userInfo['logo']);
            session('portrait',$userInfo['portrait']);
            session('background',$userInfo['background']);
            session('color',$userInfo['color']);
            if($fac_login_name == 'root'){
                session('administrator',true);
            }else{
                session('administrator',false);
            }
        }
        return $return;
	}
    //退出
	public function logout() {
        session(null);
        redirect(U('Login/index'));
    }

}